package com.xinjian.jianba.controller;

import java.io.IOException;

import org.elasticsearch.action.DocWriteResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xinjian.jianba.dto.UserOnlineDto;
import com.xinjian.jianba.dto.es.EsServiceResult;
import com.xinjian.jianba.dto.es.OneKeyOnlineDto;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.UserOnlineService;
import com.xinjian.jianba.service.ValidatorService;
import com.xinjian.jianba.utils.JsonUtil;

@RestController
@RequestMapping("/api/v3/jianba/Online")
public class UserOnlineControllor extends AbstractController{
	private static final Logger LOGGER = LoggerFactory.getLogger(UserOnlineControllor.class);
	@Autowired
	private UserOnlineService userOnlineService;
	@Autowired
	private ValidatorService validatorService;
	@PostMapping("/state")
	public String onLine(@RequestBody OneKeyOnlineDto oneKeyOnlineDto) throws IOException{
		try {
			validatorService.valid(oneKeyOnlineDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST,e.getMessage());
		}
		if(oneKeyOnlineDto.getIsOnline().equals(0)){
			LOGGER.info("下线{}",JsonUtil.getJsonString(oneKeyOnlineDto));
		}else if(oneKeyOnlineDto.getIsOnline().equals(1)){
			LOGGER.info("上线{}",JsonUtil.getJsonString(oneKeyOnlineDto));
		}
		EsServiceResult result=userOnlineService.state(oneKeyOnlineDto);
		if(result.getStatus()==EsServiceResult.ResultStatus.NOT_FOUND.getValue()||result.getStatus()==EsServiceResult.ResultStatus.UPDATED.getValue()){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		}else{
			return this.responseClientJsonData(ResponseCode.ERROR, "操作失败");
		}
	}
}
